package com.xpgk.mvc.everyDaySession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;

@Service(name = EveryDaySessionService.serviceName)
public class EveryDaySessionService  extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(EveryDaySessionService.class);

	public static final String serviceName = "everyDaySessionService";
	
	
	/**保存日会记录
	 * 
	 * @param organIds
	 * @param userIds
	 * @param tm
	 * @return
	 */
	public  String  saveDaySession(String organIds,String userIds,TodayMetting  tm)
	{	
		String  msg=ConstantRender.render_success_code;
		Date date =  new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(date);
		String problemCustomer = tm.getStr(TodayMetting.column_problemCustomer);
		String instrumentFailure = tm.getStr(TodayMetting.column_instrumentFailure);
		String hygiene = tm.getStr(TodayMetting.column_hygiene);
		String dutyProblem = tm.getStr(TodayMetting.column_dutyProblem);
		String sql = " select  *  from sb_shop_todaymetting where rhdate= ? and organIds = ?";
		TodayMetting  todayMetting = TodayMetting.dao.findFirst(sql , date1,organIds);
		if (todayMetting!=null) {
			if(problemCustomer!=null){
				todayMetting.set(TodayMetting.column_problemCustomer, problemCustomer);
			}
			if(instrumentFailure!=null){
				todayMetting.set(TodayMetting.column_instrumentFailure, instrumentFailure);
			}
			if(hygiene!=null){
				todayMetting.set(TodayMetting.column_hygiene, hygiene);
			}
			if(dutyProblem!=null){
				todayMetting.set(TodayMetting.column_dutyProblem, dutyProblem);
			}
			if(!todayMetting.update()){
				msg = "保存失败";
				return msg;
			}
			
		}else{
		
		tm.set(TodayMetting.column_organIds, organIds)
			.set(TodayMetting.column_userIds, userIds)
			.set(TodayMetting.column_rhdate,  new Date())
			.set(TodayMetting.column_gxdate, new Date());
		if(!tm.save())
		{
			log.debug("保存日会记录失败");
			throw new RuntimeException("保存日会记录失败!");
		}}
		return msg;
	}
	
	public  Map<String, Object> getOther(String date, String organIds) {
		String msg = ConstantRender.render_success_code;
		Map<String, Object> queryParam = new HashMap<String, Object>();
		Map<String, Object> mm = new HashMap<>();
		queryParam.put("date", date);
		queryParam.put("organIds", organIds);
		String sql = " select  *  from sb_shop_todaymetting where rhdate= ? and organIds = ?";
		List<Record>  cdl = Db.find(sql , date,organIds);
		if(cdl.size()!=0){
			Record r = cdl.get(0);
			mm.put("other", r);
		}else{
			msg = "没有当日信息";
		}
		mm.put("msg", msg);
		return mm;
		
		
	}
}
